<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="120px">
      <el-form-item label="样本源类型编号" prop="sourceTypeCode">
        <el-input
          v-model="queryParams.sourceTypeCode"
          clearable
          placeholder="请输入样本源类型编号"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="样本源类型名称" prop="sourceTypeName">
        <el-input
          v-model="queryParams.sourceTypeName"
          clearable
          placeholder="请输入样本源类型名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="样本源类型描述" prop="sourceTypeDesc">
        <el-input
          v-model="queryParams.sourceTypeDesc"
          clearable
          placeholder="请输入样本源类型描述"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['biobank:sourceType:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="sourceTypeList"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      row-key="sourceTypeId"
    >
      <el-table-column align="center" label="样本源类型编号" prop="sourceTypeCode" width="160"/>
      <el-table-column align="center" label="样本源类型名称" prop="sourceTypeName"/>
      <el-table-column :show-overflow-tooltip="true" align="center" label="样本源类型描述" prop="sourceTypeDesc"/>
      <el-table-column align="center" label="自定义表单" prop="formName"/>
      <el-table-column align="center" label="自定义表单Key" prop="formKey"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['biobank:sourceType:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['biobank:sourceType:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改样本源类型 对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="父样本源类型" prop="parentId">
          <treeselect v-model="form.parentId" :normalizer="normalizer" :options="sourceTypeOptions"
                      placeholder="请选择父样本源类型ID"
          />
        </el-form-item>
        <el-form-item label="样本源类型编号" prop="sourceTypeCode">
          <el-input v-model="form.sourceTypeCode" placeholder="请输入样本源类型编号"/>
        </el-form-item>
        <el-form-item label="样本源类型名称" prop="sourceTypeName">
          <el-input v-model="form.sourceTypeName" placeholder="请输入样本源类型名称"/>
        </el-form-item>
        <el-form-item label="样本源类型描述" prop="sourceTypeDesc">
          <el-input v-model="form.sourceTypeDesc" placeholder="请输入样本源类型描述"/>
        </el-form-item>
        <el-form-item label="自定义表单" prop="sourceTypeDesc">
          <!--          <el-input v-model="form.sourceTypeDesc" placeholder="请输入自定义表单Key" />-->
          <el-select
            v-model="form.formKey"
            clearable
            placeholder="自定义表单"
            size="small"
            style="width: 100%"
          >
            <el-option
              v-for="cForm in customFormOptions"
              :key="cForm.formId"
              :label="cForm.formName"
              :value="cForm.formKey"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addSourceType, delSourceType, getSourceType, listSourceType, updateSourceType } from '@/api/biobank/sourceType'
import { allForm } from '@/api/tool/form'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'SourceType',
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 样本源类型 表格数据
      sourceTypeList: [],
      // 样本源类型 树选项
      sourceTypeOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        sourceTypeCode: null,
        sourceTypeName: null,
        sourceTypeDesc: null
      },
      // 自定义表单
      customFormOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sourceTypeCode: [
          { required: true, message: '样本源类型编号不能为空', trigger: 'blur' }, {
            max: 50,
            message: '样本源类型编号不能超过50个字符',
            trigger: 'blur'
          }
        ],
        sourceTypeName: [
          { required: true, message: '样本源类型名称不能为空', trigger: 'blur' },
          { max: 50, message: '样本源类型名称不能超过50个字符', trigger: 'blur' }
        ],
        parentId: { required: true, message: '父样本源类型不能为空', trigger: 'change' },
        sourceTypeDesc: { max: 100, message: '样本源类型描述不能超过100个字符', trigger: 'blur' }
      }
    }
  },
  created() {
    this.getList()
    this.getCustomFormList()
  },
  methods: {
    /** 查询样本源类型 列表 */
    getList() {
      this.loading = true
      listSourceType(this.queryParams).then(response => {
        this.sourceTypeList = this.handleTree(response.data, 'sourceTypeId', 'parentId')
        this.loading = false
      })
    },
    getCustomFormList() {
      let queryForm = {}
      queryForm.formType = 'source'
      allForm(queryForm).then(response => {
        this.customFormOptions = response.data
      })
    },
    /** 转换样本源类型 数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.sourceTypeId,
        label: node.sourceTypeName,
        children: node.children
      }
    },
    /** 查询样本源类型下拉树结构 */
    getTreeselect() {
      listSourceType().then(response => {
        this.sourceTypeOptions = []
        const data = { sourceTypeId: 0, sourceTypeName: '顶级节点', children: [] }
        data.children = this.handleTree(response.data, 'sourceTypeId', 'parentId')
        this.sourceTypeOptions.push(data)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        sourceTypeId: null,
        parentId: null,
        sourceTypeCode: null,
        sourceTypeName: null,
        sourceTypeDesc: null,
        formKey: null,
        formName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.getTreeselect()
      this.open = true
      this.title = '添加样本源类型 '
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.getTreeselect()
      if (row != null) {
        this.form.parentId = row.sourceTypeId
      }
      getSourceType(row.sourceTypeId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改样本源类型 '
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.sourceTypeId != null) {
            updateSourceType(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addSourceType(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除所选的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delSourceType(row.sourceTypeId)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>
